package com.itheima.controller;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CampaignSetting;
import com.itheima.pojo.CampaignSetting;
import com.itheima.service.CampaignSettingService;
import com.itheima.utils.POIUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ZhaoShengChao
 * @create 2021/8/8 8:24
 */
@RequestMapping("campaignSetting")
@RestController
public class CampaignSettingController {
    @Reference
    private CampaignSettingService campaignSettingService;

    /**
     * 文件上传
     * @param excelFile
     * @return
     */

    @RequestMapping("/upload")
    public Result upload(MultipartFile excelFile){
        try {
            List<String[]> excelList = POIUtils.readExcel(excelFile);

            List<CampaignSetting> campaignSettings = new ArrayList<>();
            //循环遍历excelList 得到每一行的数据

            for (String[] strings : excelList) {
                String cpCode = strings[0];
                String helpCode = strings[1];
                String cpName = strings[2];
                String cpFrequency = strings[3];
                String cpTime = strings[4];

                campaignSettings.add(new CampaignSetting(cpCode,helpCode,cpName,cpFrequency,cpTime));
            }
            campaignSettingService.add(campaignSettings);
        } catch (IOException e) {

            return new Result(false, MessageConstant.IMPORT_CAMPAIGNSETTING_FAIL);
        }
        return new Result(true, MessageConstant.IMPORT_CAMPAIGNSETTING_SUCCESS);
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {

        return campaignSettingService.pageQuery(queryPageBean);
    }

    /**
     * 添加数据
     * @param campaignSetting
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody CampaignSetting campaignSetting){
        System.out.println(campaignSetting);
        try {
            campaignSettingService.add(campaignSetting);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.ADD_CAMPAIGNSETTING_FAIL);
        }
        return new Result(true, MessageConstant.ADD_CAMPAIGNSETTING_SUCCESS);
    }

    /**
     * 编辑
     * @param campaignSetting
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody CampaignSetting campaignSetting){
        //调用方法
        try {
            campaignSettingService.edit(campaignSetting);
        }catch(Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CAMPAIGNSETTING_FAIL);

        }
        return new Result(true,MessageConstant.EDIT_CAMPAIGNSETTING_SUCCESS);
    }

    /**
     *编辑操作
     * @param id
     * @return
     */
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @RequestMapping("/delete")
    public Result delete(Integer id){
        //调用方法
        try {
            campaignSettingService.deleteById(id);
        }catch(Exception e){
            e.printStackTrace();

            return new Result(false,MessageConstant.DELETE_CAMPAIGNSETTING_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_CAMPAIGNSETTING_SUCCESS);
    }
}
